



<!DOCTYPE html>
<html lang="zh-CN">
  <head><meta name="generator" content="Hexo 3.9.0">
    <title>TypeScript 支持 — Vue.js</title>
    <meta charset="utf-8">
    <meta name="description" content="Vue.js - The Progressive JavaScript Framework">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="alternate" hreflang="x-default" href="https://vuejs.org/v2/guide/typescript.html">
    <link rel="alternate" hreflang="zh" href="https://cn.vuejs.org/v2/guide/typescript.html">
    <link rel="alternate" hreflang="ja" href="https://jp.vuejs.org/v2/guide/typescript.html">
    <link rel="alternate" hreflang="ru" href="https://ru.vuejs.org/v2/guide/typescript.html">
    <link rel="alternate" hreflang="ko" href="https://kr.vuejs.org/v2/guide/typescript.html">
    <link rel="alternate" hreflang="pt-BR" href="https://br.vuejs.org/v2/guide/typescript.html">
    <link rel="alternate" hreflang="fr" href="https://fr.vuejs.org/v2/guide/typescript.html">
    <link rel="alternate" hreflang="es" href="https://es.vuejs.org/v2/guide/typescript.html">

    <meta property="og:type" content="article">
    <meta property="og:title" content="TypeScript 支持 — Vue.js">
    <meta property="og:description" content="Vue.js - The Progressive JavaScript Framework">
    <meta property="og:image" content="https://cn.vuejs.org/images/logo.png">

    <meta name="twitter:card" content="summary">
    <meta name="twitter:title" content="TypeScript 支持 — Vue.js">
    <meta name="twitter:description" content="Vue.js - The Progressive JavaScript Framework">
    <meta name="twitter:image" content="https://cn.vuejs.org/images/logo.png">

    <link rel="apple-touch-icon" sizes="57x57" href="/vue2/images/icons/apple-icon-57x57.png">
    <link rel="apple-touch-icon" sizes="60x60" href="/vue2/images/icons/apple-icon-60x60.png">
    <link rel="apple-touch-icon" sizes="72x72" href="/vue2/images/icons/apple-icon-72x72.png">
    <link rel="apple-touch-icon" sizes="76x76" href="/vue2/images/icons/apple-icon-76x76.png">
    <link rel="apple-touch-icon" sizes="114x114" href="/vue2/images/icons/apple-icon-114x114.png">
    <link rel="apple-touch-icon" sizes="120x120" href="/vue2/images/icons/apple-icon-120x120.png">
    <link rel="apple-touch-icon" sizes="144x144" href="/vue2/images/icons/apple-icon-144x144.png">
    <link rel="apple-touch-icon" sizes="152x152" href="/vue2/images/icons/apple-icon-152x152.png">
    <link rel="apple-touch-icon" sizes="180x180" href="/vue2/images/icons/apple-icon-180x180.png">
    <link rel="icon" type="image/png" sizes="192x192" href="/vue2/images/icons/android-icon-192x192.png">
    <link rel="icon" type="image/png" sizes="32x32" href="/vue2/images/icons/favicon-32x32.png">
    <link rel="icon" type="image/png" sizes="96x96" href="/vue2/images/icons/favicon-96x96.png">
    <link rel="icon" type="image/png" sizes="16x16" href="/vue2/images/icons/favicon-16x16.png">
    <meta name="msapplication-TileImage" content="/images/icons/ms-icon-144x144.png">
    <link rel="icon" href="/vue2/images/logo.svg">

    <meta name="msapplication-TileColor" content="#4fc08d">
    <meta name="theme-color" content="#4fc08d">

    <meta name="msapplication-config" content="browserconfig.xml">
    <link rel="manifest" href="/vue2/manifest.json">

    <!-- <link href="https://fonts.gstatic.com" rel="preconnect" crossorigin> -->
    <!-- <link href="https://maxcdn.bootstrapcdn.com" rel="preconnect" crossorigin> -->

    <!-- <link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600|Roboto Mono&display=swap" rel="stylesheet"> -->
    <!-- <link href="https://fonts.googleapis.com/css?family=Dosis:500&text=Vue.js&display=swap" rel="stylesheet"> -->

    <link href="//code.bdstatic.com/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">

    <!-- main page styles -->
    <link rel="stylesheet" href="/vue2/css/page.css">

    <!-- this needs to be loaded before guide's inline scripts -->
    <script src="/vue2/js/vue.js"></script>

    

    <script>
      Vue.config.productionTip = false
      window.PAGE_TYPE = "guide"
    </script>

    <!-- ga -->
    <script>
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

      ga('create', 'UA-46852172-3', 'cn.vuejs.org');
      ga('send', 'pageview');
    </script>

    <!-- vimeo analytics -->
    <!-- <script type="text/javascript" defer="defer" src="https://extend.vimeocdn.com/ga/72160148.js"></script> -->
</head>
  <body class="docs">

    

    <div id="mobile-bar">
      <a class="menu-button"></a>
      <a class="logo" href="/"></a>
    </div>
    <div>
  <div id="v3-banner">
    <span class="hidden-sm">非官方文档，仅为个人学习使用，查看官方文档请移步</span><a href="https://cn.vuejs.org/" target="_blank">这里</a>。
  </div>

  <header id="header">
    <a id="logo" href="/vue2/">
      <img src="/vue2/images/logo.svg" alt="vue logo">
      <span>Vue.js</span>
    </a>
    <ul id="nav">
      <li>
  <form id="search-form">
    <input type="text" id="search-query-nav" class="search-query st-default-search-input" aria-label="搜索">
  </form>
</li>
<li class="nav-dropdown-container learn">
  <a class="nav-link current">学习</a><span class="arrow"></span>
  <ul class="nav-dropdown">
    <li><h4>文档</h4></li>
    <li>
      <ul>
        <li><a href="/vue2/v2/guide/" class="nav-link current">教程</a></li>
        <li><a href="/vue2/v2/api/" class="nav-link">API</a></li>
        <li><a href="/vue2/v2/style-guide/" class="nav-link">风格指南</a></li>
        <li><a href="/vue2/v2/examples/" class="nav-link">示例</a></li>
        <li><a href="/vue2/v2/cookbook/" class="nav-link">Cookbook</a></li>
      </ul>
    </li>
    <li><h4>视频教程</h4></li>
    <li>
      <ul>
        <li>
          <a href="https://www.vuemastery.com/courses/" class="nav-link" target="_blank" rel="sponsored noopener">
            Vue Mastery (英文)
          </a>
        </li>
        <li>
          <a href="https://vueschool.io/?friend=vuejs&utm_source=Vuejs.org&utm_medium=Link&utm_content=Navbar%20Dropdown" class="nav-link" target="_blank" rel="sponsored noopener">
            Vue School (英文)
          </a>
        </li>
        <!-- start: special logic for cn -->
        <li>
          <a href="https://learning.dcloud.io/" class="nav-link" target="_blank" rel="sponsored noopener">
            DCloud 视频教程
          </a>
        </li>
        <!-- end: special logic for cn -->
      </ul>
    </li>
  </ul>
</li>

<li class="nav-dropdown-container ecosystem">
  <a class="nav-link">生态系统</a><span class="arrow"></span>
  <ul class="nav-dropdown">
    <li><h4>帮助</h4></li>
    <li><ul>
      <li><a href="https://forum.vuejs.org/" class="nav-link" target="_blank" rel="noopener">论坛</a></li>
      <li><a href="https://chat.vuejs.org/" class="nav-link" target="_blank" rel="noopener">聊天室</a></li>
      <li><a href="https://events.vuejs.org/meetups/" class="nav-link" target="_blank" rel="noopener">聚会</a></li>
    </ul></li>
    <li><h4>工具</h4></li>
    <li>
      <ul>
        <li><a href="https://github.com/vuejs/vue-devtools" class="nav-link" target="_blank" rel="noopener">Devtools</a></li>
        <li><a href="https://cli.vuejs.org/zh/" class="nav-link" target="_blank" rel="noopener">Vue CLI</a></li>
        <li><a href="https://vue-loader.vuejs.org/zh/" class="nav-link" target="_blank" rel="noopener">Vue Loader</a></li>
      </ul>
    </li>
    <li><h4>核心插件</h4></li>
    <li><ul>
      <li><a href="https://router.vuejs.org/zh/" class="nav-link" target="_blank" rel="noopener">Vue Router</a></li>
      <li><a href="https://vuex.vuejs.org/zh/" class="nav-link" target="_blank" rel="noopener">Vuex</a></li>
      <li><a href="https://ssr.vuejs.org/zh/" class="nav-link" target="_blank" rel="noopener">Vue 服务端渲染</a></li>
    </ul></li>
    <li><h4>信息</h4></li>
    <li><ul>
      <li><a href="https://news.vuejs.org" class="nav-link" target="_blank" rel="noopener">周刊</a></li>
      <li><a href="https://github.com/vuejs/vue/projects/6" class="nav-link" target="_blank" rel="noopener">Roadmap</a></li>
      <li><a href="https://events.vuejs.org/" class="nav-link" target="_blank" rel="noopener">活动</a></li>
      <li><a href="https://twitter.com/vuejs" class="nav-link" target="_blank" rel="noopener">Twitter</a></li>
      <li><a href="https://medium.com/the-vue-point" class="nav-link" target="_blank" rel="noopener">博客</a></li>
      <li><a href="https://vuejobs.com/?ref=vuejs" class="nav-link" target="_blank" rel="noopener">工作</a></li>
      <li><a href="https://dev.to/t/vue" class="nav-link" target="_blank" rel="noopener">DEV 社区</a></li>
    </ul></li>
  </ul>
</li>

<li>
  <a href="/vue2/v2/guide/team.html" class="nav-link team">团队</a>
</li>
<li class="nav-dropdown-container resources">
  <a href="#" class="nav-link">资源列表</a><span class="arrow"></span>
  <ul class="nav-dropdown">
    <li><a href="/vue2/resources/partners.html" class="nav-link">合作伙伴</a></li>
    <li><a href="/vue2/resources/themes.html" class="nav-link">主题</a></li>
    <li><a href="https://github.com/vuejs/awesome-vue" class="nav-link" target="_blank" rel="noopener">Awesome Vue</a></li>
    <li><a href="https://awesomejs.dev/for/vue/" class="nav-link" target="_blank" rel="noopener">浏览和 Vue 相关的包</a></li>
  </ul>
</li>

<!-- <li>
  <a href="/vue2/partners" class="nav-link ">Partners</a>
</li> -->
<li class="nav-dropdown-container support-vue">
  <a href="/support-vuejs/" class="nav-link">支持 Vue</a><span class="arrow"></span>
  <ul class="nav-dropdown">
    <li><a href="/support-vuejs/#One-time-Donations" class="nav-link">一次性赞助</a></li>
    <li><a href="/support-vuejs/#Recurring-Pledges" class="nav-link">周期性赞助</a></li>
    <!-- start: special logic for cn -->
    <li><a href="https://www.smallsticker.com/%E8%B4%B4%E7%BA%B8/vue.html" rel="noopener" class="nav-link">贴纸</a></li>
    <li><a href="https://osholic.com/?utm_source=vue&utm_medium=dropdown" rel="noopener" class="nav-link">周边</a></li>
    <!-- end: special logic for cn -->
    <li><a href="https://vue.threadless.com" target="_blank" rel="noopener" class="nav-link">T 恤商店</a></li>
  </ul>
</li>

<li class="nav-dropdown-container language">
  <a class="nav-link">多语言</a><span class="arrow"></span>
  <ul class="nav-dropdown">
    <li><a href="https://vuejs.org/v2/guide/typescript.html" class="nav-link">English</a></li>
    <li><a href="https://jp.vuejs.org/v2/guide/typescript.html" class="nav-link">日本語</a></li>
    <li><a href="https://ru.vuejs.org/v2/guide/typescript.html" class="nav-link">Русский</a></li>
    <li><a href="https://kr.vuejs.org/v2/guide/typescript.html" class="nav-link">한국어</a></li>
    <li><a href="https://br.vuejs.org/v2/guide/typescript.html" class="nav-link">Português</a></li>
    <li><a href="https://fr.vuejs.org/v2/guide/typescript.html" class="nav-link">Français</a></li>
    <li><a href="https://vi.vuejs.org/v2/guide/typescript.html" class="nav-link">Tiếng Việt</a></li>
    <li><a href="https://es.vuejs.org/v2/guide/typescript.html" class="nav-link">Español</a></li>
    <li><a href="https://docs.vuejs.id/v2/guide/typescript.html" class="nav-link">Bahasa Indonesia</a></li>
  </ul>
</li>

<!-- start: special logic for cn -->
<li><a href="https://github.com/vuejs/cn.vuejs.org/" target="_blank" class="nav-link contribute">参与翻译</a></li>
<!-- end: special logic for cn -->

    </ul>
  </header>
</div>

    
      <div id="main" class="fix-sidebar">
        
  

<div class="sidebar">
  <div class="sidebar-inner">
    
    <div class="list">
      <div id="sidebar-sponsors-special">
  <div class="main-sponsor">
    <!-- start: special logic for cn -->
    <span>特别赞助商</span>
    <div>
    <a href="http://www.dcloud.io/?hmsr=vuejsorg&hmpl=&hmcu=&hmkw=&hmci=" target="_blank" rel="sponsored noopener" class="logo">
      <img id="s-dcloud" alt="dcloud">
      <script>
      (function(){
        var el = document.getElementById("s-dcloud");
        var adImg = null;
        var altImg = "/vue2/images/dcloud2.png";
        var img = "/vue2/images/dcloud1.png";
        el.src = adImg || (altImg && Math.random() >= 0.5 ? altImg : img);
      })()
      </script>
    </a>
    </div>
    <div>
    <a href="https://authing.cn/welcome?from=vuejs" target="_blank" rel="sponsored noopener" class="logo">
      <img id="s-authing" alt="authing">
      <script>
      (function(){
        var el = document.getElementById("s-authing");
        var adImg = null;
        var altImg = null;
        var img = "/vue2/images/authing.svg";
        el.src = adImg || (altImg && Math.random() >= 0.5 ? altImg : img);
      })()
      </script>
    </a>
    </div>
    <div>
    <a href="https://qingfuwu.cn?utm_campaign=TR_eSJH7nqW&utm_content=&utm_medium=Web&utm_source=CH_53OxwryD&utm_term=" target="_blank" rel="sponsored noopener" class="logo">
      <img id="s-qingfuwu" alt="qingfuwu">
      <script>
      (function(){
        var el = document.getElementById("s-qingfuwu");
        var adImg = null;
        var altImg = null;
        var img = "/vue2/images/qingfuwu.svg";
        el.src = adImg || (altImg && Math.random() >= 0.5 ? altImg : img);
      })()
      </script>
    </a>
    </div>
    <!-- end: special logic for cn -->
  </div>
</div>
<!--
<div id="sidebar-sponsors-platinum-left">
  <div class="main-sponsor">
    <span>白金赞助商</span>
    <div>
    <a href="https://vueschool.io/?utm_source=Vuejs.org&utm_medium=Banner&utm_campaign=Sponsored%20Banner&utm_content=V1" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/vue2/images/vueschool.png" alt="VueSchool">
    </a>
    <a href="https://vehikl.com/" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/vue2/images/vehikl.png" alt="Vehikl">
    </a>
    <a href="https://retool.com/?utm_source=sponsor&utm_campaign=vue" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/vue2/images/retool.png" alt="Retool">
    </a>
    <a href="https://passionatepeople.io/" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/vue2/images/passionate_people.png" alt="Passionate People">
    </a>
    <a href="https://www.storyblok.com" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/vue2/images/storyblok.png" alt="Storyblok">
    </a>
    <a href="https://ionicframework.com/vue?utm_source=partner&utm_medium=referral&utm_campaign=vuesponsorship&utm_content=vuedocs" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/vue2/images/ionic.png" alt="Ionic">
    </a>
    <a href="https://nuxtjs.org/" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/vue2/images/nuxt.png" alt="Nuxt">
    </a>
    </div>
  </div>
  <a class="become-backer" href="/vue2/support-vuejs">
    成为赞助者
  </a>
</div>

-->

      
        <h2>
          
          教程
          
            <select class="version-select">
              <option value="v3">3.x</option>
              <option value="SELF" selected>2.x</option>
              <option value="v1">1.0</option>
              <option value="012">0.12</option>
              <option value="011">0.11</option>
            </select>
          
        </h2>
        <ul class="menu-root">
  
    
    
      
        <li><h3>基础</h3></li>
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/installation.html" class="sidebar-link">安装</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/index.html" class="sidebar-link">介绍</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/instance.html" class="sidebar-link">Vue 实例</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/syntax.html" class="sidebar-link">模板语法</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/computed.html" class="sidebar-link">计算属性和侦听器</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/class-and-style.html" class="sidebar-link">Class 与 Style 绑定</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/conditional.html" class="sidebar-link">条件渲染</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/list.html" class="sidebar-link">列表渲染</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/events.html" class="sidebar-link">事件处理</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/forms.html" class="sidebar-link">表单输入绑定</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/components.html" class="sidebar-link">组件基础</a>
    </li>
  
    
    
      
      
        <li><h3>深入了解组件</h3></li>
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/components-registration.html" class="sidebar-link">组件注册</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/components-props.html" class="sidebar-link">Prop</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/components-custom-events.html" class="sidebar-link">自定义事件</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/components-slots.html" class="sidebar-link">插槽</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/components-dynamic-async.html" class="sidebar-link">动态组件 & 异步组件</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/components-edge-cases.html" class="sidebar-link">处理边界情况</a>
    </li>
  
    
    
      
      
      
        <li><h3>过渡 & 动画</h3></li>
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/transitions.html" class="sidebar-link">进入/离开 & 列表过渡</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/transitioning-state.html" class="sidebar-link">状态过渡</a>
    </li>
  
    
    
      
      
      
      
        <li><h3>可复用性 & 组合</h3></li>
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/mixins.html" class="sidebar-link">混入</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/custom-directive.html" class="sidebar-link">自定义指令</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/render-function.html" class="sidebar-link">渲染函数 & JSX</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/plugins.html" class="sidebar-link">插件</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/filters.html" class="sidebar-link">过滤器</a>
    </li>
  
    
    
      
      
      
      
      
        <li><h3>工具</h3></li>
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/single-file-components.html" class="sidebar-link">单文件组件</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/testing.html" class="sidebar-link">测试</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/typescript.html" class="sidebar-link current">TypeScript 支持</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/deployment.html" class="sidebar-link">生产环境部署</a>
    </li>
  
    
    
      
      
      
      
      
      
        <li><h3>规模化</h3></li>
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/routing.html" class="sidebar-link">路由</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/state-management.html" class="sidebar-link">状态管理</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/ssr.html" class="sidebar-link">服务端渲染</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/security.html" class="sidebar-link">安全</a>
    </li>
  
    
    
      
      
      
      
      
      
      
        <li><h3>内在</h3></li>
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/reactivity.html" class="sidebar-link">深入响应式原理</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
        <li><h3>迁移</h3></li>
      
      
    
    <li>
      <a href="/vue2/v2/guide/migration.html" class="sidebar-link">从 Vue 1.x 迁移</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/migration-vue-router.html" class="sidebar-link">从 Vue Router 0.7.x 迁移</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/migration-vuex.html" class="sidebar-link">从 Vuex 0.6.x 迁移到 1.0</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
        <li><h3>更多</h3></li>
      
    
    <li>
      <a href="/vue2/v2/guide/comparison.html" class="sidebar-link">对比其他框架</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/join.html" class="sidebar-link">加入 Vue.js 社区</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/team.html" class="sidebar-link">认识团队</a>
    </li>
  
</ul>

      
    </div>
  </div>
</div>
<!--

<div id="sidebar-sponsors-platinum-right">
  <div class="main-sponsor">
    <span>白金赞助商</span>
    <div>
    <a href="https://vueschool.io/?utm_source=Vuejs.org&utm_medium=Banner&utm_campaign=Sponsored%20Banner&utm_content=V1" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/vue2/images/vueschool.png" alt="VueSchool">
    </a>
    <a href="https://vehikl.com/" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/vue2/images/vehikl.png" alt="Vehikl">
    </a>
    <a href="https://retool.com/?utm_source=sponsor&utm_campaign=vue" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/vue2/images/retool.png" alt="Retool">
    </a>
    <a href="https://passionatepeople.io/" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/vue2/images/passionate_people.png" alt="Passionate People">
    </a>
    <a href="https://www.storyblok.com" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/vue2/images/storyblok.png" alt="Storyblok">
    </a>
    <a href="https://ionicframework.com/vue?utm_source=partner&utm_medium=referral&utm_campaign=vuesponsorship&utm_content=vuedocs" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/vue2/images/ionic.png" alt="Ionic">
    </a>
    <a href="https://nuxtjs.org/" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/vue2/images/nuxt.png" alt="Nuxt">
    </a>
    </div>
  </div>
  <a class="become-backer" href="/vue2/support-vuejs">
    成为赞助者
  </a>
</div>


-->


<div class="content guide with-sidebar typescript-guide">
  <p class="tip warning v3-warning">
    您在浏览的是 v2.x 及更早版本的文档。v3.x 的文档<a href="https://wangyubina.github.io/vue3/">在这里</a>。
  </p>

  
    
      
<div id="ad">
  <!-- start: special logic for cn -->
  <div class="wwads-cn wwads-vertical" data-id="53" style="max-width: 200px"></div>
  <script type="text/javascript" src="https://cdn.wwads.cn/js/makemoney.js" async></script>
  <!-- end: special logic for cn -->
  <!-- <script
    async
    type="text/javascript"
    src="//cdn.carbonads.com/carbon.js?serve=CEBDT27Y&placement=vuejsorg"
    id="_carbonads_js"
  ></script> -->
</div>


    
  
  
    <h1>TypeScript 支持</h1>
  

  <!-- start: special logic for cn -->
  <!-- start: special logic for cn -->
<!-- <div class="ad-pagetop">
  <a href="http://www.vueshop.com.cn/index.php?http_referer=vuejs" target="_blank" rel="noopener noreferrer">
    <span class="name">VUEshop</span>
    <span class="description">中国免费商城系统 - 搜豹商城系统 - 免费50小时 Vue 视频教程</span>
    <span class="action">立即查看 &gt;</span>
  </a>
</div> -->
<!-- end: special logic for cn -->

  <!-- end: special logic for cn -->

  
    <blockquote>
<p><a href="https://cli.vuejs.org" target="_blank" rel="noopener">Vue CLI</a> 提供了内建的 TypeScript 工具支持。</p>
</blockquote>
<h2 id="发布为-NPM-包的官方声明文件"><a href="#发布为-NPM-包的官方声明文件" class="headerlink" title="发布为 NPM 包的官方声明文件"></a>发布为 NPM 包的官方声明文件</h2><p>静态类型系统能帮助你有效防止许多潜在的运行时错误，而且随着你的应用日渐丰满会更加显著。这就是为什么 Vue 不仅仅为 Vue core 提供了针对 <a href="https://www.typescriptlang.org/" target="_blank" rel="noopener">TypeScript</a> 的<a href="https://github.com/vuejs/vue/tree/dev/types" target="_blank" rel="noopener">官方类型声明</a>，还为 <a href="https://github.com/vuejs/vue-router/tree/dev/types" target="_blank" rel="noopener">Vue Router</a> 和 <a href="https://github.com/vuejs/vuex/tree/dev/types" target="_blank" rel="noopener">Vuex</a> 也提供了相应的声明文件。</p>
<p>而且，我们已经把它们<a href="https://cdn.jsdelivr.net/npm/vue@2/types/" target="_blank" rel="noopener">发布到了 NPM</a>，最新版本的 TypeScript 也知道该如何自己从 NPM 包里解析类型声明。这意味着只要你成功地通过 NPM 安装了，就不再需要任何额外的工具辅助，即可在 Vue 中使用 TypeScript 了。</p>
<h2 id="推荐配置"><a href="#推荐配置" class="headerlink" title="推荐配置"></a>推荐配置</h2><pre><code class="hljs js"><span class="hljs-comment">// tsconfig.json</span>
&#123;
  <span class="hljs-string">"compilerOptions"</span>: &#123;
    <span class="hljs-comment">// 与 Vue 的浏览器支持保持一致</span>
    <span class="hljs-string">"target"</span>: <span class="hljs-string">"es5"</span>,
    <span class="hljs-comment">// 这可以对 `this` 上的数据 property 进行更严格的推断</span>
    <span class="hljs-string">"strict"</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-comment">// 如果使用 webpack 2+ 或 rollup，可以利用 tree-shake:</span>
    <span class="hljs-string">"module"</span>: <span class="hljs-string">"es2015"</span>,
    <span class="hljs-string">"moduleResolution"</span>: <span class="hljs-string">"node"</span>
  &#125;
&#125;</code></pre>
<p>注意你需要引入 <code>strict: true</code> (或者至少 <code>noImplicitThis: true</code>，这是 <code>strict</code> 模式的一部分) 以利用组件方法中 <code>this</code> 的类型检查，否则它会始终被看作 <code>any</code> 类型。</p>
<p>参阅 <a href="https://www.typescriptlang.org/docs/handbook/compiler-options.html" target="_blank" rel="noopener">TypeScript 编译器选项文档 (英)</a> 了解更多。</p>
<h2 id="开发工具链"><a href="#开发工具链" class="headerlink" title="开发工具链"></a>开发工具链</h2><h3 id="工程创建"><a href="#工程创建" class="headerlink" title="工程创建"></a>工程创建</h3><p><a href="https://github.com/vuejs/vue-cli" target="_blank" rel="noopener">Vue CLI 3</a> 可以使用 TypeScript 生成新工程。创建方式：</p>
<pre><code class="hljs bash"><span class="hljs-comment"># 1. 如果没有安装 Vue CLI 就先安装</span>
npm install --global @vue/cli

<span class="hljs-comment"># 2. 创建一个新工程，并选择 "Manually select features (手动选择特性)" 选项</span>
vue create my-project-name</code></pre>
<h3 id="编辑器支持"><a href="#编辑器支持" class="headerlink" title="编辑器支持"></a>编辑器支持</h3><p>要使用 TypeScript 开发 Vue 应用程序，我们强烈建议您使用 <a href="https://code.visualstudio.com/" target="_blank" rel="noopener">Visual Studio Code</a>，它为 TypeScript 提供了极好的“开箱即用”支持。如果你正在使用<a href="./single-file-components.html">单文件组件</a> (SFC)，可以安装提供 SFC 支持以及其他更多实用功能的 <a href="https://github.com/vuejs/vetur" target="_blank" rel="noopener">Vetur 插件</a>。</p>
<p><a href="https://www.jetbrains.com/webstorm/" target="_blank" rel="noopener">WebStorm</a> 同样为 TypeScript 和 Vue 提供了“开箱即用”的支持。</p>
<h2 id="基本用法"><a href="#基本用法" class="headerlink" title="基本用法"></a>基本用法</h2><p>要让 TypeScript 正确推断 Vue 组件选项中的类型，您需要使用 <code>Vue.component</code> 或 <code>Vue.extend</code> 定义组件：</p>
<pre><code class="hljs ts"><span class="hljs-keyword">import</span> Vue <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>
<span class="hljs-keyword">const</span> Component = Vue.extend(&#123;
  <span class="hljs-comment">// 类型推断已启用</span>
&#125;)

<span class="hljs-keyword">const</span> Component = &#123;
  <span class="hljs-comment">// 这里不会有类型推断，</span>
  <span class="hljs-comment">// 因为 TypeScript 不能确认这是 Vue 组件的选项</span>
&#125;</code></pre>
<h2 id="基于类的-Vue-组件"><a href="#基于类的-Vue-组件" class="headerlink" title="基于类的 Vue 组件"></a>基于类的 Vue 组件</h2><p>如果您在声明组件时更喜欢基于类的 API，则可以使用官方维护的 <a href="https://github.com/vuejs/vue-class-component" target="_blank" rel="noopener">vue-class-component</a> 装饰器：</p>
<pre><code class="hljs ts"><span class="hljs-keyword">import</span> Vue <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>
<span class="hljs-keyword">import</span> Component <span class="hljs-keyword">from</span> <span class="hljs-string">'vue-class-component'</span>

<span class="hljs-comment">// @Component 修饰符注明了此类为一个 Vue 组件</span>
<span class="hljs-meta">@Component</span>(&#123;
  <span class="hljs-comment">// 所有的组件选项都可以放在这里</span>
  template: <span class="hljs-string">'&lt;button @click="onClick"&gt;Click!&lt;/button&gt;'</span>
&#125;)
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-keyword">class</span> MyComponent <span class="hljs-keyword">extends</span> Vue &#123;
  <span class="hljs-comment">// 初始数据可以直接声明为实例的 property</span>
  message: <span class="hljs-built_in">string</span> = <span class="hljs-string">'Hello!'</span>

  <span class="hljs-comment">// 组件方法也可以直接声明为实例的方法</span>
  onClick (): <span class="hljs-built_in">void</span> &#123;
    <span class="hljs-built_in">window</span>.alert(<span class="hljs-keyword">this</span>.message)
  &#125;
&#125;</code></pre>
<h2 id="增强类型以配合插件使用"><a href="#增强类型以配合插件使用" class="headerlink" title="增强类型以配合插件使用"></a>增强类型以配合插件使用</h2><p>插件可以增加 Vue 的全局/实例 property 和组件选项。在这些情况下，在 TypeScript 中制作插件需要类型声明。庆幸的是，TypeScript 有一个特性来补充现有的类型，叫做<a href="https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation" target="_blank" rel="noopener">模块补充 (module augmentation)</a>。</p>
<p>例如，声明一个 <code>string</code> 类型的实例 property <code>$myProperty</code>：</p>
<pre><code class="hljs ts"><span class="hljs-comment">// 1. 确保在声明补充的类型之前导入 'vue'</span>
<span class="hljs-keyword">import</span> Vue <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>

<span class="hljs-comment">// 2. 定制一个文件，设置你想要补充的类型</span>
<span class="hljs-comment">//    在 types/vue.d.ts 里 Vue 有构造函数类型</span>
<span class="hljs-keyword">declare</span> <span class="hljs-keyword">module</span> 'vue/types/vue' &#123;
<span class="hljs-comment">// 3. 声明为 Vue 补充的东西</span>
  <span class="hljs-keyword">interface</span> Vue &#123;
    $myProperty: <span class="hljs-built_in">string</span>
  &#125;
&#125;</code></pre>
<p>在你的项目中包含了上述作为声明文件的代码之后 (像 <code>my-property.d.ts</code>)，你就可以在 Vue 实例上使用 <code>$myProperty</code> 了。</p>
<pre><code class="hljs ts"><span class="hljs-keyword">var</span> vm = <span class="hljs-keyword">new</span> Vue()
<span class="hljs-built_in">console</span>.log(vm.$myProperty) <span class="hljs-comment">// 将会顺利编译通过</span></code></pre>
<p>你也可以声明额外的 property 和组件选项：</p>
<pre><code class="hljs ts"><span class="hljs-keyword">import</span> Vue <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>

<span class="hljs-keyword">declare</span> <span class="hljs-keyword">module</span> 'vue/types/vue' &#123;
  <span class="hljs-comment">// 可以使用 `VueConstructor` 接口</span>
  <span class="hljs-comment">// 来声明全局 property</span>
  <span class="hljs-keyword">interface</span> VueConstructor &#123;
    $myGlobal: <span class="hljs-built_in">string</span>
  &#125;
&#125;

<span class="hljs-comment">// ComponentOptions 声明于 types/options.d.ts 之中</span>
<span class="hljs-keyword">declare</span> <span class="hljs-keyword">module</span> 'vue/types/options' &#123;
  <span class="hljs-keyword">interface</span> ComponentOptions&lt;V <span class="hljs-keyword">extends</span> Vue&gt; &#123;
    myOption?: <span class="hljs-built_in">string</span>
  &#125;
&#125;</code></pre>
<p>上述的声明允许下面的代码顺利编译通过：</p>
<pre><code class="hljs ts"><span class="hljs-comment">// 全局 property</span>
<span class="hljs-built_in">console</span>.log(Vue.$myGlobal)

<span class="hljs-comment">// 额外的组件选项</span>
<span class="hljs-keyword">var</span> vm = <span class="hljs-keyword">new</span> Vue(&#123;
  myOption: <span class="hljs-string">'Hello'</span>
&#125;)</code></pre>
<h2 id="标注返回值"><a href="#标注返回值" class="headerlink" title="标注返回值"></a>标注返回值</h2><p>因为 Vue 的声明文件天生就具有循环性，TypeScript 可能在推断某个方法的类型的时候存在困难。因此，你可能需要在 <code>render</code> 或 <code>computed</code> 里的方法上标注返回值。</p>
<pre><code class="hljs ts"><span class="hljs-keyword">import</span> Vue, &#123; VNode &#125; <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>

<span class="hljs-keyword">const</span> Component = Vue.extend(&#123;
  data () &#123;
    <span class="hljs-keyword">return</span> &#123;
      msg: <span class="hljs-string">'Hello'</span>
    &#125;
  &#125;,
  methods: &#123;
    <span class="hljs-comment">// 需要标注有 `this` 参与运算的返回值类型</span>
    greet (): <span class="hljs-built_in">string</span> &#123;
      <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.msg + <span class="hljs-string">' world'</span>
    &#125;
  &#125;,
  computed: &#123;
    <span class="hljs-comment">// 需要标注</span>
    greeting(): <span class="hljs-built_in">string</span> &#123;
      <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.greet() + <span class="hljs-string">'!'</span>
    &#125;
  &#125;,
  <span class="hljs-comment">// `createElement` 是可推导的，但是 `render` 需要返回值类型</span>
  render (createElement): VNode &#123;
    <span class="hljs-keyword">return</span> createElement(<span class="hljs-string">'div'</span>, <span class="hljs-keyword">this</span>.greeting)
  &#125;
&#125;)</code></pre>
<p>如果你发现类型推导或成员补齐不工作了，标注某个方法也许可以帮助你解决这个问题。使用 <code>--noImplicitAny</code> 选项将会帮助你找到这些未标注的方法。</p>
<h2 id="标注-Prop"><a href="#标注-Prop" class="headerlink" title="标注 Prop"></a>标注 Prop</h2><pre><code class="hljs ts"><span class="hljs-keyword">import</span> Vue, &#123; PropType &#125; <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>

<span class="hljs-keyword">interface</span> ComplexMessage &#123;
  title: <span class="hljs-built_in">string</span>,
  okMessage: <span class="hljs-built_in">string</span>,
  cancelMessage: <span class="hljs-built_in">string</span>
&#125;
<span class="hljs-keyword">const</span> Component = Vue.extend(&#123;
  props: &#123;
    name: <span class="hljs-built_in">String</span>,
    success: &#123; <span class="hljs-keyword">type</span>: <span class="hljs-built_in">String</span> &#125;,
    callback: &#123;
      <span class="hljs-keyword">type</span>: <span class="hljs-built_in">Function</span> <span class="hljs-keyword">as</span> PropType&lt;<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> <span class="hljs-built_in">void</span>&gt;
    &#125;,
    message: &#123;
      <span class="hljs-keyword">type</span>: <span class="hljs-built_in">Object</span> <span class="hljs-keyword">as</span> PropType&lt;ComplexMessage&gt;,
      required: <span class="hljs-literal">true</span>,
      validator (message: ComplexMessage) &#123;
        <span class="hljs-keyword">return</span> !!message.title;
      &#125;
    &#125;
  &#125;
&#125;)</code></pre>
<p>如果你发现校验器并没有得到类型推导或命名补全不工作，用预期的类型标注参数可能会助你解决这类问题。</p>

  
  
    <div class="guide-links">
      
      
        <span>← <a href="/vue2/v2/guide/testing.html">测试</a></span>
      
      
      
        <span style="float: right;"><a href="/vue2/v2/guide/deployment.html">生产环境部署</a> →</span>
      
    </div>
  
  <div class="footer">
      <script src="//m.servedby-buysellads.com/monetization.js" type="text/javascript"></script>
<div id="bsa-native"></div>
<script>
_bsa.init('custom', 'CKYD62QM', 'placement:vuejsorg',
  {
    target: '#bsa-native',
    template: '<a class="native-box" href="##statlink##"><div class="native-sponsor">Sponsor</div><div class="native-text"><strong>##company##</strong> — ##description##</div></a>'
  }
);
</script>


    发现错误？想参与编辑？
    <a href="https://github.com/vuejs/cn.vuejs.org/blob/master/src/v2/guide/typescript.md" rel="noopener" target="_blank">
      在 GitHub 上编辑此页！
    </a>
    <!-- Deployed on
    <a href="https://url.netlify.com/HJ8X2mxP8" rel="noopener" target="_blank">
      Netlify
    </a>. -->
  </div>
</div>

      </div>
      <script src="/vue2/js/smooth-scroll.min.js"></script>
    

    <!-- main custom script for sidebars, version selects etc. -->
    <script src="/vue2/js/css.escape.js"></script>
    <script src="/vue2/js/common.js"></script>

    <!-- search -->
    <link href="//code.bdstatic.com/npm/docsearch.js@1.5.0/dist/cdn/docsearch.min.css" rel="stylesheet" type="text/css">
    <link rel="stylesheet" href="/vue2/css/search.css">
    <script src="//code.bdstatic.com/npm/docsearch.js@1.5.0/dist/cdn/docsearch.min.js"></script>
    <script>
    [
      '#search-query-nav',
      '#search-query-sidebar',
      '#search-query-menu'
    ].forEach(function (selector) {
      if (!document.querySelector(selector)) return
      // search index defaults to v2
      var match = window.location.pathname.match(/^\/(v\d+)/)
      var version = match ? match[1] : 'v2'
      docsearch({
      appId: 'BH4D9OD16A',
      apiKey: '5638280abff9d207417bb03be05f0b25',
      indexName: 'vuejs_cn2',
      inputSelector: selector,
      algoliaOptions: { facetFilters: ["version:" + version] },
      autocompleteOptions: { hint: false, appendTo: 'body'}
      })
    })
    </script>
  </body>
</html>
